﻿using System;
using System.Drawing;
using System.Linq.Expressions;
using DevExpress.XtraBars;
using DevExpress.XtraNavBar;
using Loki.Utils;

namespace Loki.UI.Win
{
    public class NavigationBinder : Binder
    {
        public void Bind<TModel>(NavBarControl navBar, Expression<Func<TModel, object>> propertyGetter) where TModel : class
        {
            var containerModel = GetContainer<TModel>(navBar, propertyGetter);
            if (containerModel == null)
            {
                return;
            }

            foreach (var navigationItem in containerModel.Children)
            {
                NavBarGroup group = new NavBarGroup();
                var navParent = navigationItem as IParent;
                if (navParent != null)
                {
                    group.Caption = GetName(navParent);

                    navBar.Groups.Add(group);

                    foreach (var navigationLink in navParent.Children)
                    {
                        var navLink = navigationLink as INavigationElement;
                        if (navLink != null)
                        {
                            var item = group.AddItem();
                            item.Item.Caption = GetName(navLink);

                            var navMessage = navigationLink as IMessageElement;
                            if (navMessage != null)
                            {
                                item.Item.LinkClicked += (s, o) => Toolkit.MessageBus.BeginPublishOnUIThread(navMessage.Message);
                            }

                            var navCommand = navigationLink as ICommandElement;
                            if (navCommand != null)
                            {
                                item.Item.LinkClicked += (s, e) => navCommand.Command.Execute(e);

                                if (GlyphConverter != null)
                                {
                                    item.Item.SmallImage = GlyphConverter.Convert(navCommand.Command, typeof(Image), true, UIContext.Windows.Culture) as Image;
                                    item.Item.SmallImage = GlyphConverter.Convert(navCommand.Command, typeof(Image), false, UIContext.Windows.Culture) as Image;
                                }
                            }
                        }
                    }
                }
            }
        }

        public void Bind<TModel>(BarManager manager, Bar bar, Expression<Func<TModel, object>> propertyGetter) where TModel : class
        {
            var form = manager.Form;
            if (form == null)
            {
                Log.Warn("Bar manager is not associated with a form");
                return;
            }

            var containerModel = GetContainer<TModel>(form, propertyGetter);
            if (containerModel == null)
            {
                return;
            }

            foreach (var menuItem in containerModel.Children)
            {
                BarButtonItem button = new BarButtonItem();
                var menuName = menuItem as ICommandElement;
                if (menuName != null)
                {
                    button.Caption = GetName(menuName);
                    button.ItemClick += (s, e) => menuName.Command.Execute(e);

                    if (GlyphConverter != null)
                    {
                        button.Glyph = GlyphConverter.Convert(menuName.Command, typeof(Image), true, UIContext.Windows.Culture) as Image;
                        button.Glyph = GlyphConverter.Convert(menuName.Command, typeof(Image), false, UIContext.Windows.Culture) as Image;
                    }

                    manager.Items.Add(button);
                    bar.ItemLinks.Add(button);
                }
            }
        }
    }
}